package it.polimi.meteocal.web.beans;

import it.polimi.meteocal.ejb.eventmanagement.EventDTO;
import it.polimi.meteocal.ejb.eventmanagement.EventMgr;
import it.polimi.meteocal.ejb.eventmanagement.Event;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
 
import org.primefaces.event.ScheduleEntryMoveEvent;
import org.primefaces.event.ScheduleEntryResizeEvent;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.DefaultScheduleEvent;
import org.primefaces.model.DefaultScheduleModel;
import org.primefaces.model.ScheduleEvent;
import org.primefaces.model.ScheduleModel;
 
@ManagedBean(name="meteoCalendar")
@ViewScoped
public class MeteoCalendar implements Serializable {

		private ScheduleModel eventModel;
	 
	    private ScheduleEvent event = new DefaultScheduleEvent();
	    
	    @EJB
	    private EventMgr eventMgr;
	    
	    private List<Event> list;
	    
	    private int magic;
	 
	    @PostConstruct //Post construct cause error 
	    public void init() {
	    	eventModel = new DefaultScheduleModel();
	    }
	    
	    private Calendar today() {
	        Calendar calendar = Calendar.getInstance();
	        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE), 0, 0, 0);
	 
	        return calendar;
	    }
	     
	    public Date getRandomDate(Date base) {
	        Calendar date = Calendar.getInstance();
	        date.setTime(base);
	        date.add(Calendar.DATE, ((int) (Math.random()*30)) + 1);    //set random day of month
	         
	        return date.getTime();
	    }
	     
	    public Date getInitialDate() {
	        Calendar calendar = Calendar.getInstance();
	        calendar.set(calendar.get(Calendar.YEAR), Calendar.FEBRUARY, calendar.get(Calendar.DATE), 0, 0, 0);
	         
	        return calendar.getTime();
	    }
	    
	    public ScheduleModel getEventModel() {
	        return eventModel;
	    }
	     
	    public ScheduleEvent getEvent() {
	        return event;
	    }
	 
	    public void setEvent(ScheduleEvent event) {
	        this.event = event;
	    }
	     
	    public void addEvent(ActionEvent actionEvent) {
	        if(event.getId() == null)
	            eventModel.addEvent(event);
	        else
	            eventModel.updateEvent(event);
	         
	        event = new DefaultScheduleEvent();
	    }
	     
	    public void onEventSelect(SelectEvent selectEvent) {
	        event = (ScheduleEvent) selectEvent.getObject();
	    }
	     
	    public void onDateSelect(SelectEvent selectEvent) {
	        event = new DefaultScheduleEvent("", (Date) selectEvent.getObject(), (Date) selectEvent.getObject());
	        Date date = event.getStartDate();
	        
	        Calendar cal = Calendar.getInstance();
	        int today = cal.get(Calendar.DAY_OF_MONTH);
	        
	        //
	        System.out.println("today: "+today);
	        //
	        
	        cal.setTime(date);
	        
	        int selectedDay = cal.get(Calendar.DAY_OF_MONTH)+1;
	        
	        //
	        System.out.println("selected day: "+selectedDay);
	        //
	        
	        magic = selectedDay - today;
	        /*if (magic<0){
	        	magic=0;
	        }*/
	        
	        //
	        System.out.println("magic: "+magic);
	        //
	    }
	     
	    public void onEventMove(ScheduleEntryMoveEvent event) {
	        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Event moved", "Day delta:" + event.getDayDelta() + ", Minute delta:" + event.getMinuteDelta());
	         
	        addMessage(message);
	    }
	     
	    public void onEventResize(ScheduleEntryResizeEvent event) {
	        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Event resized", "Day delta:" + event.getDayDelta() + ", Minute delta:" + event.getMinuteDelta());
	         
	        addMessage(message);
	    }
	     
	    private void addMessage(FacesMessage message) {
	        FacesContext.getCurrentInstance().addMessage(null, message);
	    }

		public int getMagic() {
			return magic;
		}

		public void setMagic(int magic) {
			this.magic = magic;
		}
	    
	    
	    
	}